package com.example.snack1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import java.util.ArrayList;


public class FirstPageActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener{
    private static final String TAG = "MyList3MainActivity";
    ListView listView;
    SnackAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_page);
        listView = findViewById(R.id.mylist3);

        ProgressBar progressBar = findViewById(R.id.progressBar);

        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);

        Handler handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 11) {

                    adapter = new SnackAdapter(FirstPageActivity.this, R.layout.list_item, (ArrayList
                            <FirstSnack>) msg.obj);
                    listView.setAdapter(adapter);

                    //显示隐藏
                    listView.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.GONE);
                }
                super.handleMessage(msg);
            }
        };
        FirstTask task = new FirstTask();
        task.setHandler(handler);
        Thread t = new Thread(task);
        t.start();
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Object itemAtPosition = listView.getItemAtPosition(position);
        //HashMap<String, String> map = (HashMap<String, String>) itemAtPosition;
        FirstSnack item =(FirstSnack) listView.getItemAtPosition(position);

        //String countryStr = map.get("country");
        //String rateStr = map.get("rate");
        String countryStr=item.getCountry();
        String rateStr=item.getRate();

        Log.i(TAG, "onItemClick:countryStr" + countryStr);
        Log.i(TAG, "onItemClick:rateStr" + rateStr);
        // adapter.remove(map);

        //show(countryStr,rateStr);

    }
/*
    private void show(String countryStr, String rateStr) {
        Intent config = new Intent(this, CountRateActivity.class)
                .putExtra("count_country", countryStr)
                .putExtra("count_rate", rateStr);
        Log.i(TAG, "openCountRate:count_country=" + countryStr);
        Log.i(TAG, "openCountRate:count_rate=" + rateStr);
        //Log.i(TAG,"openConfig:dollarRate:",dollarRate);
        startActivity(config);
    }

 */

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemLongClick:长按事件 position="+position);
        FirstSnack item =(FirstSnack) listView.getItemAtPosition(position);
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("提示")
                .setMessage("请确认是否删除当前数据")
                .setPositiveButton("是",new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i(TAG,"onClick:对话框事件处理");
                        //删除数据项
                        adapter.remove(item);
                    }
                }).setNegativeButton("否",null);
        builder.create().show();
        //adapter.remove(item);
        return true;//false:长按后会触发单击事件
    }
}

